package com.mxpio.erp.inventory.job;

import cn.hutool.core.date.DateUtil;
import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.InventoryEnums;
import com.mxpio.erp.common.inventory.entity.*;
import com.mxpio.erp.common.inventory.service.InventoryPlanService;
import com.mxpio.erp.common.inventory.service.VInvLotStockService;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.policy.impl.CrudType;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.CriteriaUtils;
import com.mxpioframework.jpa.query.Operator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Classname GenInventoryJob
 * @Description 盘点计划执行 job
 */
@Slf4j
@Component
public class GenInventoryJob implements Job {

    @Autowired
    private InventoryPlanService planService;

    @Autowired
    private VInvLotStockService vInvLotStockService;
    @Override
    @Transactional
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info(" --- 生成盘点任务调度开始 --- ");

        // 查询出要生成盘点任务的计划
        Criteria planCri = Criteria.create().addCriterion("isEnable", Operator.EQ, 1);
        List<InventoryPlan> list = planService.list(InventoryPlan.class, planCri);
        if (CollectionUtils.isEmpty(list)) {
            log.error("生效的盘点计划为空！");
            return;
        }
        String formatToday = DateUtil.formatDate(new Date());//yyyy-MM-dd

        for (InventoryPlan plan : list) {
            String planType = plan.getPlanType();//计划类型：标准静态、循环、动态
            String endLoopType = plan.getEndLoopType();//结束类型
            Integer isExcludeZero = plan.getIsExcludeZero();//库存为0不参与盘点
            //生成criteria
            Criteria scopeCri = null;
            if (StringUtils.isNotBlank(plan.getCriteria())){
                scopeCri =CriteriaUtils.json2Criteria(plan.getCriteria());
            }
            if (scopeCri==null){
                scopeCri = Criteria.create();
            }
            scopeCri.addCriterion("whCode",Operator.EQ,plan.getWhCode());//拼装仓单过滤条件
            if (isExcludeZero!=null&&isExcludeZero==1){
                scopeCri.addCriterion("quantity",Operator.GT,0);//库存大于0
            }

            if (StringUtils.equals(planType, InventoryEnums.PlanType.STAND.getCode())){//静态
                Date startTime = plan.getStartDate();//计划开始执行的日期
                if (DateUtil.compare(new Date(),startTime)<0) {//没到计划开始执行日期时，跳过
                    continue;//跳过
                }
                Date lastPlanEnd = plan.getLastPlanEnd();
                Date cycleStartTime = null;
                Date cycleEndTime = null;
                if (lastPlanEnd!=null&&DateUtil.compare(new Date(),lastPlanEnd) < 0){
                    continue;//上次计划结束时间 不等于空，且小于当前日期，跳过，
                }
                String formatPlanStartTime = formatToday+" "+"00:00:00";
                cycleStartTime = DateUtil.parse(formatPlanStartTime);
                if (StringUtils.equals(plan.getRateUnit(),InventoryEnums.PlanRateUnit.DAY.getCode())){
                    cycleEndTime = DateUtils.addDays(cycleStartTime, 1 * Integer.valueOf(plan.getRate()));
                    cycleEndTime = DateUtils.addSeconds(cycleEndTime, -1);
                }else if (StringUtils.equals(plan.getRateUnit(),InventoryEnums.PlanRateUnit.MONTH.getCode())){
                    cycleEndTime = DateUtils.addMonths(cycleStartTime, 1 * Integer.valueOf(plan.getRate()));
                    cycleEndTime = DateUtils.addSeconds(cycleEndTime, -1);
                }
                /*String planEndTimeStr = DateUtil.formatDateTime(cycleEndTime);
                cycleEndTime =  DateUtil.parse(planEndTimeStr);*/

                boolean planFinishFlag = false;
                if (StringUtils.equals(endLoopType, InventoryEnums.PlanEndLoopType.FREQUENCY.getCode())) {//按次数
                    Integer endTimes = plan.getEndTimes();//结束条件
                    Long count = JpaUtil.linq(Inventory.class).equal("planCode", plan.getCode()).count();//已经生成的任务数量
                    if (count>=endTimes){//任务数量小于循环次数时，才生成盘点单
                        continue;
                    }
                    if(endTimes-count==1){
                        planFinishFlag = true;
                    }
                } else if (StringUtils.equals(endLoopType, InventoryEnums.PlanEndLoopType.TIMING.getCode())) {//按时间
                    Date endDate = plan.getEndDate();//定时结束日期
                    if (DateUtil.compare(cycleStartTime,endDate)>0){//本次开始循环日期小于结束日期
                        continue;
                    }
                    if (DateUtil.compare(cycleEndTime,endDate)>=0){
                        planFinishFlag = true;
                    }
                } /*else if (StringUtils.equals(endLoopType, InventoryEnums.PlanEndLoopType.LOOP.getCode())) {//按循环 仅生成下个周期的任务
                    if (!StringUtils.equals(plan.getExecStatus(),InventoryEnums.PlanExecStatus.EXECING.getCode())){
                        plan.setExecStatus(InventoryEnums.PlanExecStatus.EXECING.getCode());//执行中
                    }
                    planService.update(plan);
                }*/

                if (planFinishFlag){
                    plan.setIsEnable(Integer.parseInt(CommonEnums.YesNo.NO.getCode()));//计划状态更改
                    plan.setExecStatus(InventoryEnums.PlanExecStatus.FINISH.getCode());//已完成
                }else{
                    if (!StringUtils.equals(plan.getExecStatus(),InventoryEnums.PlanExecStatus.EXECING.getCode())){
                        plan.setExecStatus(InventoryEnums.PlanExecStatus.EXECING.getCode());//执行中
                    }
                }
                plan.setLastPlanStart(cycleStartTime);
                plan.setLastPlanEnd(cycleEndTime);
                planService.update(plan);

                List<VInvLotStock> invFlows = vInvLotStockService.listByLot(scopeCri);
                if (invFlows.size()>0){
                    //生产盘点单
                    Inventory baseInfoFromPlan = getBaseInfoFromPlan(plan);
                    baseInfoFromPlan.setBusiDate(new Date());
                    JpaUtil.save(baseInfoFromPlan);
                    int i = 0;
                    Set<String> itemCodeSet = invFlows.stream().map(VInvLotStock::getItemCode).collect(Collectors.toSet());
                    List<Item> dbItemList = JpaUtil.linq(Item.class).in("itemCode", itemCodeSet).list();
                    Map<String,Item> dbItemMap = JpaUtil.index(dbItemList, "itemCode");
                    for (VInvLotStock stock:invFlows){
                        i++;
                        Item dbItem = null;
                        if (dbItemMap.containsKey(stock.getItemCode())){
                            dbItem = dbItemMap.get(stock.getItemCode());
                        }else{
                            //throw new MBootException("库存流水中的物料"+stock.getItemCode()+"未匹配到物料基础信息！");
                            log.error("库存流水中的物料"+stock.getItemCode()+"未匹配到物料基础信息，不能生成对应的盘点单明细！");
                            continue;
                        }
                        InventoryItem item = new InventoryItem();
                        item.setInventoryCode(baseInfoFromPlan.getInventoryCode());
                        item.setItemCode(stock.getItemCode());
                        item.setLotNo(stock.getLotNo());
                        item.setSubLotIndex(stock.getSubLotIndex());//批次索引号
                        item.setWhCode(stock.getWhCode());
                        /*item.setRegionCode(stock.getRegionCode());
                        item.setAreaCode(stock.getAreaCode());
                        item.setLocationCode(stock.getLocationCode());*/
                        item.setQuantity(stock.getQuantity());
                        item.setLineNo(i);
                        //关联物料冗余信息字段
                        item.setItemName(dbItem.getItemName());
                        item.setItemSpec(dbItem.getItemSpec());
                        item.setItemGroupCode(dbItem.getItemGroupCode());
                        item.setUnitCode(dbItem.getUnitCode());
                        item.setDrawingNo(dbItem.getDrawingNo());
                        item.setCrudType(CrudType.SAVE);
                        item.setPlanGenerate(true);//标识明细由计划生成，和手工添加明细的数据做区分
                        if (StringUtils.equals(InventoryEnums.InventoryWay.BLIND.getCode(),plan.getInventoryWay())){//盲盘
                            item.setQuantityStr("*");
                        }else{//明盘
                            item.setQuantityStr(item.getQuantity().toPlainString());
                        }
                        JpaUtil.save(item);
                    }
                }else{
                    log.info("计划"+plan.getCode()+"中数据维度没有对应的库存数据，不能生成对应盘点任务！");
                    continue;
                }
            }else if (StringUtils.equals(planType, InventoryEnums.PlanType.DYNAMIC.getCode())){//动态盘点
                Date startTime = plan.getStartDate();//计划开始执行的日期
                if (DateUtil.compare(new Date(),startTime)<0) {//没到计划开始执行日期时，跳过
                    continue;//跳过
                }
                Date lastPlanEnd = plan.getLastPlanEnd();
                Date lastPlanStart = plan.getLastPlanStart();
                if (lastPlanEnd==null){//为空时，说明计划未执行，此时取第一个计划周期
                    String formatFirstPlanStartTime = DateUtil.formatDate(startTime)+" "+"00:00:00";
                    Date firstCycleStartTime = DateUtil.parse(formatFirstPlanStartTime);
                    Date firstCycleEndTime = null;
                    if (StringUtils.equals(plan.getRateUnit(),InventoryEnums.PlanRateUnit.DAY.getCode())){
                        firstCycleEndTime = DateUtils.addDays(firstCycleStartTime, 1 * Integer.valueOf(plan.getRate()));
                        firstCycleEndTime = DateUtils.addSeconds(firstCycleEndTime, -1);
                    }else if (StringUtils.equals(plan.getRateUnit(),InventoryEnums.PlanRateUnit.MONTH.getCode())){
                        firstCycleEndTime = DateUtils.addMonths(firstCycleStartTime, 1 * Integer.valueOf(plan.getRate()));
                        firstCycleEndTime = DateUtils.addSeconds(firstCycleEndTime, -1);
                    }
                    lastPlanEnd =  firstCycleEndTime;
                    lastPlanStart = firstCycleStartTime;
                }
                if (lastPlanEnd!=null&&DateUtil.compare(new Date(),lastPlanEnd) < 0){
                    continue;//上次计划结束时间 不等于空，且小于当前日期，跳过，
                }
                Criteria modifyFlowCri = Criteria.create();
                modifyFlowCri.addCriterion("createTime",Operator.GE,lastPlanStart);
                modifyFlowCri.addCriterion("createTime",Operator.LE,lastPlanEnd);
                List<InvFlow> modifyFlowList = JpaUtil.linq(InvFlow.class).equal("whCode", plan.getWhCode()).where(modifyFlowCri).list();
                if (modifyFlowList.isEmpty()){
                    log.error("当前周期内【"+DateUtil.formatDate(lastPlanStart)+"~"+DateUtil.formatDate(lastPlanEnd)+"】，库存未发生变化，不产生对应的动态盘点单！");
                    continue;
                }
                scopeCri.addCriterion("lotNo",Operator.IN,modifyFlowList.stream().map(InvFlow::getLotNo).collect(Collectors.toSet()));//批次发生变化的范围

                Date cycleStartTime = null;
                Date cycleEndTime = null;
                String formatPlanStartTime = formatToday+" "+"00:00:00";
                cycleStartTime = DateUtil.parse(formatPlanStartTime);
                if (StringUtils.equals(plan.getRateUnit(),InventoryEnums.PlanRateUnit.DAY.getCode())){
                    cycleEndTime = DateUtils.addDays(cycleStartTime, 1 * Integer.valueOf(plan.getRate()));
                    cycleEndTime = DateUtils.addSeconds(cycleEndTime, -1);
                }else if (StringUtils.equals(plan.getRateUnit(),InventoryEnums.PlanRateUnit.MONTH.getCode())){
                    cycleEndTime = DateUtils.addMonths(cycleStartTime, 1 * Integer.valueOf(plan.getRate()));
                    cycleEndTime = DateUtils.addSeconds(cycleEndTime, -1);
                }

                boolean planFinishFlag = false;
                if (StringUtils.equals(endLoopType, InventoryEnums.PlanEndLoopType.FREQUENCY.getCode())) {//按次数
                    Integer endTimes = plan.getEndTimes();//结束条件
                    Long count = JpaUtil.linq(Inventory.class).equal("planCode", plan.getCode()).count();//已经生成的任务数量
                    if (count>=endTimes){//任务数量小于循环次数时，才生成盘点单
                        continue;
                    }
                    if(endTimes-count==1){
                        planFinishFlag = true;
                    }
                } else if (StringUtils.equals(endLoopType, InventoryEnums.PlanEndLoopType.TIMING.getCode())) {//按时间
                    Date endDate = plan.getEndDate();//定时结束日期
                    if (DateUtil.compare(cycleStartTime,endDate)>0){//本次开始循环日期小于结束日期
                        continue;
                    }
                    if (DateUtil.compare(cycleEndTime,endDate)>=0){
                        planFinishFlag = true;
                    }
                } /*else if (StringUtils.equals(endLoopType, InventoryEnums.PlanEndLoopType.LOOP.getCode())) {//按循环 仅生成下个周期的任务
                    if (!StringUtils.equals(plan.getExecStatus(),InventoryEnums.PlanExecStatus.EXECING.getCode())){
                        plan.setExecStatus(InventoryEnums.PlanExecStatus.EXECING.getCode());//执行中
                    }
                    planService.update(plan);
                }*/

                if (planFinishFlag){
                    plan.setIsEnable(Integer.parseInt(CommonEnums.YesNo.NO.getCode()));//计划状态更改
                    plan.setExecStatus(InventoryEnums.PlanExecStatus.FINISH.getCode());//已完成
                }else{
                    if (!StringUtils.equals(plan.getExecStatus(),InventoryEnums.PlanExecStatus.EXECING.getCode())){
                        plan.setExecStatus(InventoryEnums.PlanExecStatus.EXECING.getCode());//执行中
                    }
                }
                plan.setLastPlanStart(cycleStartTime);
                plan.setLastPlanEnd(cycleEndTime);
                planService.update(plan);

                List<VInvLotStock> invFlows = vInvLotStockService.listByLot(scopeCri);//查询批次库存数据
                if (invFlows.size()>0){
                    //生产盘点单
                    Inventory baseInfoFromPlan = getBaseInfoFromPlan(plan);
                    baseInfoFromPlan.setBusiDate(new Date());
                    JpaUtil.save(baseInfoFromPlan);
                    int i = 0;
                    Set<String> itemCodeSet = invFlows.stream().map(VInvLotStock::getItemCode).collect(Collectors.toSet());
                    List<Item> dbItemList = JpaUtil.linq(Item.class).in("itemCode", itemCodeSet).list();
                    Map<String,Item> dbItemMap = JpaUtil.index(dbItemList, "itemCode");
                    for (VInvLotStock stock:invFlows){
                        i++;
                        Item dbItem = null;
                        if (dbItemMap.containsKey(stock.getItemCode())){
                            dbItem = dbItemMap.get(stock.getItemCode());
                        }else{
                            //throw new MBootException("库存流水中的物料"+stock.getItemCode()+"未匹配到物料基础信息！");
                            log.error("库存流水中的物料"+stock.getItemCode()+"未匹配到物料基础信息，不能生成对应的盘点单明细！");
                            continue;
                        }
                        InventoryItem item = new InventoryItem();
                        item.setInventoryCode(baseInfoFromPlan.getInventoryCode());
                        item.setItemCode(stock.getItemCode());
                        item.setLotNo(stock.getLotNo());
                        item.setSubLotIndex(stock.getSubLotIndex());//批次索引号
                        item.setWhCode(stock.getWhCode());
                        /*item.setRegionCode(stock.getRegionCode());
                        item.setAreaCode(stock.getAreaCode());
                        item.setLocationCode(stock.getLocationCode());*/
                        item.setQuantity(stock.getQuantity());
                        item.setLineNo(i);
                        //关联物料冗余信息字段
                        item.setItemName(dbItem.getItemName());
                        item.setItemSpec(dbItem.getItemSpec());
                        item.setItemGroupCode(dbItem.getItemGroupCode());
                        item.setUnitCode(dbItem.getUnitCode());
                        item.setDrawingNo(dbItem.getDrawingNo());
                        item.setCrudType(CrudType.SAVE);
                        item.setPlanGenerate(true);//标识明细由计划生成，和手工添加明细的数据做区分
                        if (StringUtils.equals(InventoryEnums.InventoryWay.BLIND.getCode(),plan.getInventoryWay())){//盲盘
                            item.setQuantityStr("*");
                        }else{//明盘
                            item.setQuantityStr(item.getQuantity().toPlainString());
                        }
                        JpaUtil.save(item);
                    }
                }else{
                    log.info("计划"+plan.getCode()+"中数据维度没有对应的库存数据，不能生成对应盘点任务！");
                    continue;
                }
            }else if (StringUtils.equals(planType, InventoryEnums.PlanType.CYCLE.getCode())){//循环盘点
                Date startTime = plan.getStartDate();//计划开始执行的日期
                if (DateUtil.compare(new Date(),startTime)<0) {//没到计划开始执行日期时，跳过
                    continue;//跳过
                }
                Date lastPlanEnd = plan.getLastPlanEnd();
                Date cycleStartTime = null;
                Date cycleEndTime = null;
                if (lastPlanEnd!=null&&DateUtil.compare(new Date(),lastPlanEnd) < 0){
                    continue;//上次计划结束时间 不等于空，且小于当前日期，跳过，
                }
                String formatPlanStartTime = formatToday+" "+"00:00:00";
                cycleStartTime = DateUtil.parse(formatPlanStartTime);
                if (StringUtils.equals(plan.getRateUnit(),InventoryEnums.PlanRateUnit.DAY.getCode())){
                    cycleEndTime = DateUtils.addDays(cycleStartTime, 1 * Integer.valueOf(plan.getRate()));
                    cycleEndTime = DateUtils.addSeconds(cycleEndTime, -1);
                }else if (StringUtils.equals(plan.getRateUnit(),InventoryEnums.PlanRateUnit.MONTH.getCode())){
                    cycleEndTime = DateUtils.addMonths(cycleStartTime, 1 * Integer.valueOf(plan.getRate()));
                    cycleEndTime = DateUtils.addSeconds(cycleEndTime, -1);
                }

                boolean planFinishFlag = false;

                Integer cyclePercentage = plan.getCyclePercentage();//每次循环比例 百分比的整数
                String cycledItemCodes = plan.getCycledItemCodes();//已经循环过的物料编码
                Long count = JpaUtil.linq(Inventory.class).equal("planCode", plan.getCode()).count();//已经生成的任务数量
                List<VInvLotStock> invFlows = vInvLotStockService.listByLot(scopeCri);
                if (invFlows.isEmpty()){
                    log.info("计划"+plan.getCode()+"中数据维度没有对应的库存数据，不能生成对应盘点任务！");
                    continue;
                }

                Set<String> itemCodeSetAll = invFlows.stream().map(VInvLotStock::getItemCode).collect(Collectors.toSet());
                List<String> itemCodeAllList = new ArrayList<>(itemCodeSetAll);
                List<String> pickedItemCodeList = new ArrayList<>();
                List<VInvLotStock> pickedInvFlows = new ArrayList<>();
                int allCnt = itemCodeAllList.size();
                Integer cycleCnt = new BigDecimal(allCnt + "").multiply(new BigDecimal(cyclePercentage + "")).divide(new BigDecimal("100"), 0, BigDecimal.ROUND_HALF_UP).intValue();//本次循环取的物料条数
                if (cycleCnt==0){//四舍五入，可能存在等零的情况，
                    cycleCnt=1;
                }
                int loopReset = 100-cyclePercentage*count.intValue();
                if (loopReset<=cyclePercentage||allCnt==cycleCnt){//最后一次，结束
                    planFinishFlag = true;
                }
                if (!planFinishFlag){
                    //未结束之前
                    if (StringUtils.isNotBlank(cycledItemCodes)){
                        List<String> dbCycledItemCodesList = Stream.of(cycledItemCodes.split(",")).collect(Collectors.toList());
                        pickedItemCodeList.addAll(dbCycledItemCodesList);//合并
                    }
                    Iterator<String> iterator = itemCodeAllList.iterator();
                    while(cycleCnt>0 && iterator.hasNext()){
                        String itemcodeStr = iterator.next();
                        if (pickedItemCodeList.contains(itemcodeStr)){
                            continue;
                        }else{
                            cycleCnt--;
                            pickedItemCodeList.add(itemcodeStr);
                            List<VInvLotStock> collect = invFlows.stream().filter(lotStock -> lotStock.getItemCode().equals(itemcodeStr)).collect(Collectors.toList());
                            pickedInvFlows.addAll(collect);
                        }
                    }
                }else{
                    if (StringUtils.isNotBlank(cycledItemCodes)){
                        List<String> dbCycledItemCodesList = Stream.of(cycledItemCodes.split(",")).collect(Collectors.toList());
                        scopeCri.addCriterion("itemCode",Operator.NOT_IN,dbCycledItemCodesList);
                    }
                    pickedInvFlows = vInvLotStockService.listByLot(scopeCri);
                }

                if (planFinishFlag){
                    plan.setIsEnable(Integer.parseInt(CommonEnums.YesNo.NO.getCode()));//计划状态更改
                    plan.setExecStatus(InventoryEnums.PlanExecStatus.FINISH.getCode());//已完成
                }else{
                    if (!StringUtils.equals(plan.getExecStatus(),InventoryEnums.PlanExecStatus.EXECING.getCode())){
                        plan.setExecStatus(InventoryEnums.PlanExecStatus.EXECING.getCode());//执行中
                    }
                    plan.setCycledItemCodes(String.join(",", pickedItemCodeList));
                }
                plan.setLastPlanStart(cycleStartTime);
                plan.setLastPlanEnd(cycleEndTime);
                planService.update(plan);
                //生产盘点单
                Inventory baseInfoFromPlan = getBaseInfoFromPlan(plan);
                baseInfoFromPlan.setBusiDate(new Date());
                JpaUtil.save(baseInfoFromPlan);

                if (pickedInvFlows.size()>0){
                    int i = 0;
                    Set<String> itemCodeSet = pickedInvFlows.stream().map(VInvLotStock::getItemCode).collect(Collectors.toSet());
                    List<Item> dbItemList = JpaUtil.linq(Item.class).in("itemCode", itemCodeSet).list();
                    Map<String,Item> dbItemMap = JpaUtil.index(dbItemList, "itemCode");
                    for (VInvLotStock stock:pickedInvFlows){
                        i++;
                        Item dbItem = null;
                        if (dbItemMap.containsKey(stock.getItemCode())){
                            dbItem = dbItemMap.get(stock.getItemCode());
                        }else{
                            //throw new MBootException("库存流水中的物料"+stock.getItemCode()+"未匹配到物料基础信息！");
                            log.error("库存流水中的物料"+stock.getItemCode()+"未匹配到物料基础信息，不能生成对应的盘点单明细！");
                            continue;
                        }
                        InventoryItem item = new InventoryItem();
                        item.setInventoryCode(baseInfoFromPlan.getInventoryCode());
                        item.setItemCode(stock.getItemCode());
                        item.setLotNo(stock.getLotNo());
                        item.setSubLotIndex(stock.getSubLotIndex());//批次索引号
                        item.setWhCode(stock.getWhCode());
                        /*item.setRegionCode(stock.getRegionCode());
                        item.setAreaCode(stock.getAreaCode());
                        item.setLocationCode(stock.getLocationCode());*/
                        item.setQuantity(stock.getQuantity());
                        item.setLineNo(i);
                        //关联物料冗余信息字段
                        item.setItemName(dbItem.getItemName());
                        item.setItemSpec(dbItem.getItemSpec());
                        item.setItemGroupCode(dbItem.getItemGroupCode());
                        item.setUnitCode(dbItem.getUnitCode());
                        item.setDrawingNo(dbItem.getDrawingNo());
                        item.setPlanGenerate(true);//标识明细由计划生成，和手工添加明细的数据做区分
                        if (StringUtils.equals(InventoryEnums.InventoryWay.BLIND.getCode(),plan.getInventoryWay())){//盲盘
                            item.setQuantityStr("*");
                        }else{//明盘
                            item.setQuantityStr(item.getQuantity().toPlainString());
                        }
                        JpaUtil.save(item);
                    }
                }
            }
        }
    }

    private Inventory getBaseInfoFromPlan(InventoryPlan plan){
        Inventory inventory = new Inventory();
        inventory.setPlanCode(plan.getCode());
        inventory.setInventoryStatus(InventoryEnums.InventoryStatus.CREATE.getStatusCode());
        inventory.setInventoryType(plan.getInventoryType());
        inventory.setInventoryWay(plan.getInventoryWay());
        inventory.setInventoryRange(plan.getInventoryRange());
        inventory.setInventoryPercentage(plan.getInventoryPercentage());
        inventory.setInventoryRangeBasis(plan.getInventoryRangeBasis());
        inventory.setWhCode(plan.getWhCode());
        inventory.setInventoryPersons(plan.getInventoryPersons());
        inventory.setIsAppend(plan.getIsAppend());
        inventory.setInventoryPlanDate(plan.getLastPlanStart());
        inventory.setCrudType(CrudType.SAVE);
        return inventory;
    }
}
